<template>
  <div>
    <div class="plugin-video">
      <div
        tabindex="0"
        class="el-upload el-upload--text"
        @click.prevent.stop="videoBoxHandle"
      >
        <video
          v-if="modelValue"
          ref="myVideoRef"
          class="video"
          :src="getImgSrc"
          controls="controls"
        />
        <el-icon
          v-else
          class="pic-uploader-icon"
        >
          <Plus />
        </el-icon>
      </div>
    </div>
    <!-- 弹窗, 新增视频 -->
    <video-box
      v-if="videoBoxVisible"
      ref="videoBoxRef"
      @refresh-pic="refreshPic"
    />
  </div>
</template>

<script setup>
const emit = defineEmits(['update:modelValue'])

const resourcesUrl = import.meta.env.VITE_APP_RESOURCES_URL
const props = defineProps({
  modelValue: {
    default: '',
    type: String
  },
  disabled: {
    default: false,
    type: Boolean
  }
})

// 打开视频选择窗
const videoBoxVisible = ref(false)
const videoBoxRef = ref(null)
const videoBoxHandle = () => {
  videoBoxVisible.value = true
  nextTick(() => {
    videoBoxRef.value.init(1)
  })
}

/**
 * 获取视频路径
 */
const getImgSrc = computed(() => {
  if (!props.modelValue) {
    return ''
  }
  return resourcesUrl + props.modelValue
})

const refreshPic = (imagePath) => {
  emit('update:modelValue', imagePath)
}

</script>

<style lang="scss" scoped>
.plugin-video {
  display: inline-block;
  width: auto;
  .el-upload {
    display: flex;
    align-items: center;
    justify-content: center;
    width: auto;
    min-width: 120px;
    max-width: 300px;
    height: auto;
    min-height: 120px;
    max-height: 250px;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    .pic-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: auto;
      height: auto;
      text-align: center;
    }
    .video {
      width: auto;
      max-width: 300px;
      // height: 120px;
      height: auto;
      max-height: 250px;
      display: block;
    }
    .el-upload:hover {
      border-color: #409eff;
    }
  }
}
</style>
